/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package olympicgameslogic;

import java.io.Serializable;
import java.util.Collections;

/**
 *
 * @author 75
 */
public class EditionDisciplineEvent implements Serializable{

   private Edition edition;
   private DisciplineEvent editionDisciplineEvent;
   private LinkedList<EditionEventParticipation> editionEventParticipation;
   private boolean medalsAssigned = false;

   public EditionDisciplineEvent() {
   }

   public EditionDisciplineEvent(Edition edition, DisciplineEvent editionDisciplineEvent, LinkedList<EditionEventParticipation> editionEventParticipation) {
      this.edition = edition;
      this.editionDisciplineEvent = editionDisciplineEvent;
      this.editionEventParticipation = editionEventParticipation;
   }

   /**
    * @return the edition
    */
   public Edition getEdition() {
      return edition;
   }

   /**
    * @return the editionDisciplineEvent
    */
   public DisciplineEvent getEditionDisciplineEvent() {
      return editionDisciplineEvent;
   }

   /**
    * @return the editionEventParticipation
    */
   public LinkedList<EditionEventParticipation> getEditionEventParticipation() {
      return editionEventParticipation;
   }

   /**
    * @param edition the edition to set
    */
   public void setEdition(Edition edition) {
      this.edition = edition;
   }

   /**
    * @param editionDisciplineEvent the editionDisciplineEvent to set
    */
   public void setEditionDisciplineEvent(DisciplineEvent editionDisciplineEvent) {
      this.editionDisciplineEvent = editionDisciplineEvent;
   }

   /**
    * @param editionEventParticipation the editionEventParticipation to set
    */
   public void setEditionEventParticipation(LinkedList<EditionEventParticipation> editionEventParticipation) {
      this.editionEventParticipation = editionEventParticipation;
   }

   public void addEditionEventParticipation(EditionEventParticipation eventParticipation) {
      editionEventParticipation.add(eventParticipation);
   }

   public void sortResults() {

      Collections.sort(editionEventParticipation);

   }

   public Discipline getDiscipline() {
      return this.editionDisciplineEvent.getEventDiscipline();
   }

   public Modality getModality() {
      return this.editionDisciplineEvent.getEventDiscipline().getModality();
   }

   @Override
   public String toString() {
      return "Ano: " + edition.getYear() + ", " + getEditionDisciplineEvent().toString();
   }

   public EditionEventAthleteParticipation getAthleteParticipation(Athlete a) {
      for (EditionEventParticipation edEvPart : editionEventParticipation) {
	 if (((EditionEventAthleteParticipation) edEvPart).getAthlete() == a) {
	    return (EditionEventAthleteParticipation) edEvPart;
	 }
      }
      return null;
   }

   public EditionEventTeamParticipation getTeamParticipation(Team t) {
      for (EditionEventParticipation edEvPart : editionEventParticipation) {
	 if (((EditionEventTeamParticipation) edEvPart).getTeam() == t) {
	    return (EditionEventTeamParticipation) edEvPart;
	 }
      }
      return null;

   }

   public void assignMedals() {



      Collections.sort(editionEventParticipation);
      if (medalsAssigned) {
	 System.out.println("\n\n\n-------------------------------------\nsISSO querias tu. já as atribui!\n\n\n-------------------------------------------");
	 return;
      }
      if (this.getEditionDisciplineEvent().getEventDiscipline().getType().equals(Discipline.DISCIPLINETYPE.INDIVIDUAL)) {
	 /*
	  * se atribuir mais que três medalhas já não vai ver a próxima exceto
	  * se o resultado for igual. para isso precisamos sempre de saber o
	  * resultado seguinte uma vez que a nossa lista já está ordenada para
	  * atribuição de medalhas
	  *
	  * Enquato for possível carregar o próximo temos sempre que comparar
	  * com o atual para definir a medalhas a atribuir
	  *
	  *
	  */

	 EditionEventAthleteParticipation edEvAtPart, edEvPartAux;
	 Result rCurrent, rNext;
	 int numMedalsAssigned = 0;
	 int medalIndex = 0; //First Medal is always GOLD
	 int indexFora = 0;
	 for (indexFora = 0; indexFora < editionEventParticipation.size(); indexFora++) {
	    if (medalIndex > 2 || numMedalsAssigned > 2) {
	       break;
	    }
	    edEvAtPart = (EditionEventAthleteParticipation) editionEventParticipation.getElement(indexFora);
	    rCurrent = edEvAtPart.getResult();
	    edEvAtPart.assignMedal(Medal.medals[medalIndex]);
	    numMedalsAssigned++;
	  //  System.out.println("\n\n..........indexfor " + indexFora + " " + rCurrent);
	    for (int j = (indexFora + 1); j < editionEventParticipation.size(); j++) {

	       edEvPartAux = (EditionEventAthleteParticipation) editionEventParticipation.getElement(j);
	       rNext = edEvPartAux.getResult();
	   //    System.out.println("\n\n..........index J " + j + " " + rNext);
	       if (rCurrent.isEqual(rNext)) {
		  //System.out.println(rCurrent + " " + rNext);
		  edEvPartAux.assignMedal(Medal.medals[medalIndex]);
		  numMedalsAssigned++;

	       } else {
		  medalIndex++;
		  indexFora = j - 1;
		  break;
	       }
	    }

	 }
      } else if (this.getEditionDisciplineEvent().getEventDiscipline().getType().equals(Discipline.DISCIPLINETYPE.TEAM)) {
	 EditionEventTeamParticipation edEvTPart, edEvPartAux;
	 Result rCurrent, rNext;
	 int numMedalsAssigned = 0;
	 int medalIndex = 0; //First Medal is always GOLD
	 int indexFora = 0;
	 for (indexFora = 0; indexFora < editionEventParticipation.size(); indexFora++) {
	    if (medalIndex > 2 || numMedalsAssigned > 2) {
	       break;
	    }
	    edEvTPart = (EditionEventTeamParticipation) editionEventParticipation.getElement(indexFora);
	    rCurrent = edEvTPart.getResult();
	    edEvTPart.assignMedal(Medal.medals[medalIndex]);
	    numMedalsAssigned++;
	   // System.out.println("\n\n..........indexfor " + indexFora + " " + rCurrent);
	    for (int j = (indexFora + 1); j < editionEventParticipation.size(); j++) {

	       edEvPartAux = (EditionEventTeamParticipation) editionEventParticipation.getElement(j);
	       rNext = edEvPartAux.getResult();
	     //  System.out.println("\n\n..........index J " + j + " " + rNext);
	       if (rCurrent.isEqual(rNext)) {
		//  System.out.println(rCurrent + " " + rNext);
		  edEvPartAux.assignMedal(Medal.medals[medalIndex]);
		  numMedalsAssigned++;

	       } else {
		  medalIndex++;
		  indexFora = j - 1;
		  break;
	       }
	    }

	 }


      }

      this.medalsAssigned = true;

   }

   //necessary to remove all the medal previously given whe the eventparticipations arebeing updated.
  

   /**
    * @return the medalsAssigned
    */
   public boolean isMedalsAssigned() {
      return medalsAssigned;
   }

   /**
    * @param medalsAssigned the medalsAssigned to set
    */
   public void setMedalsAssigned(boolean medalsAssigned) {
      this.medalsAssigned = medalsAssigned;
   }
}
